package com.quectel.cms.controller.forum;


import cn.hutool.core.lang.func.LambdaUtil;
import com.quectel.cms.controller.BaseController;
import com.quectel.cms.util.SessionHolder;
import com.quectel.constant.global.SystemConstants;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.core.module.forum.dto.mongo.ForumPersonCountDto;
import com.quectel.core.module.forum.service.mongo.ForumPersonCountService;
import com.quectel.core.module.mobileuser.dto.MobileUserDto;
import com.quectel.core.module.mobileuser.service.MobileUserService;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangheng
 * @email nolan.zhang@quectel.com
 * @date 2023/05/10 11:21
 */
@RestController
@RequestMapping("forumPersonCount")
@Tag(name = "ForumPostController", description = "邻友圈互动统计相关api")
public class ForumPersonCountController extends BaseController {

    @DubboReference
    private ForumPersonCountService forumPersonCountService;
    @DubboReference
    private MobileUserService mobileUserService;


    /**
     * 分页查询
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("list")
    @RequirePermissions(values = "forumPersonCount:list")
    @Operation(summary = "分页获取发帖管理列表")
    public Response<Page<ForumPersonCountDto>> list(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize,
            @Parameter(description = "手机号,全匹配") @RequestParam String mobile

    ) {
        //查询列表数据
        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        super.fillAuthorityData(request.getParams());
        if (StringUtils.isNotBlank(mobile)) {
            MobileUserDto mobileUserDto = mobileUserService.selectByMobile(mobile);
            if (mobileUserDto == null) {
                return Response.<Page<ForumPersonCountDto>>ok().wrap(Page.empty(request.getPageNo(), request.getPageSize()));
            }
            request.put(LambdaUtil.getFieldName(ForumPersonCountDto::getMobileUserId), mobileUserDto.getId());
        }

        int total = forumPersonCountService.queryTotal(request.getParams());

        List<ForumPersonCountDto> list = null;
        if (total > 0) {
            list = forumPersonCountService.queryList(request.getParams());
        } else {
            list = new ArrayList<>();
        }

        Page<ForumPersonCountDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<ForumPersonCountDto>>ok().wrap(page);
    }


    /**
     * 详情
     *
     * @param id
     * @return
     */
    @GetMapping("info/{id}")
    @RequirePermissions(values = "forumPersonCount:info")
    @Operation(summary = "根据id查询详情的详情")
    public Response<ForumPersonCountDto> info(@PathVariable("id") Long id) {

        ForumPersonCountDto forumPersonCountDto = forumPersonCountService.selectById(id);
        if (forumPersonCountDto == null) {
            return Response.<ForumPersonCountDto>ok().wrap(null);
        }
        if (!forumPersonCountDto.getTenantId().equals(SessionHolder.getSysUser().getTenantId())) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        MobileUserDto mobileUserDto = mobileUserService.selectCacheById(forumPersonCountDto.getMobileUserId());
        forumPersonCountDto.setName(mobileUserDto.getName());
        forumPersonCountDto.setMobile(mobileUserDto.getMobile());
        forumPersonCountDto.setHeadImg(mobileUserDto.getHeadImg());

        return Response.<ForumPersonCountDto>ok().wrap(forumPersonCountDto);
    }
}
